package com.haiblee.aescipher;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText contentEdit = (EditText) findViewById(R.id.content_text);
        final EditText pwdEdit = (EditText) findViewById(R.id.password_text);
        final TextView resultText = (TextView) findViewById(R.id.result_text);
        findViewById(R.id.encrypt_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String content = contentEdit.getText().toString();
                final String password = pwdEdit.getText().toString();
                if(TextUtils.isEmpty(content) || TextUtils.isEmpty(password)){
                    Toast.makeText(MainActivity.this, "内容或密钥不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            final String encryptResult = HaibleeAES.execute(content,password,true);
                            resultText.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "加密成功", Toast.LENGTH_SHORT).show();
                                    resultText.setTextColor(Color.BLACK);
                                    resultText.setText(encryptResult);
                                }
                            });
                        } catch (final Exception e) {
                            resultText.post(new Runnable() {
                                @Override
                                public void run() {
                                    resultText.setTextColor(Color.RED);
                                    resultText.setText("异常：\n"+e.getClass().getName()+"\n"+e.getMessage());
                                }
                            });
                        }
                    }
                }.start();
            }
        });
        findViewById(R.id.decrypt_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String content = contentEdit.getText().toString();
                final String password = pwdEdit.getText().toString();
                if(TextUtils.isEmpty(content) || TextUtils.isEmpty(password)){
                    Toast.makeText(MainActivity.this, "内容或密钥不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            final String decryptResult = HaibleeAES.execute(content,password,false);
                            resultText.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "解密成功", Toast.LENGTH_SHORT).show();
                                    resultText.setTextColor(Color.BLACK);
                                    resultText.setText(decryptResult);
                                }
                            });
                        } catch (final Exception e) {
                            resultText.post(new Runnable() {
                                @Override
                                public void run() {
                                    resultText.setTextColor(Color.RED);
                                    resultText.setText("异常：\n"+e.getClass().getName()+"\n"+e.getMessage());
                                }
                            });
                        }
                    }
                }.start();
            }
        });
    }

    public static void main(String[] args){
        HaibleeAES.main(args);
    }
}
